unit DataItem;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes;

type
  TDataItem = class
  private
    FItems:   TList;
    FProps:   TStringList;
    FDeleted: boolean;
    FData:    TObject;
    procedure SetDeleted(AValue: Boolean);
  public
    procedure SetProp(AName: string; AData: TDataItem);
    function  GetProp(AName: string): TDataItem;
    function  Add(AData: TObject): TDataItem;
    procedure AddList(ADatas: TList);
    constructor Create;
    destructor Destroy; override;
  public
    property Deleted: boolean read FDeleted write SetDeleted;
    property Data: TObject read FData write FData;
  public
    //
  end;


implementation

{ TDataList }

function TDataItem.Add(AData: TObject): TDataItem;
begin
  result := TDataItem.Create;
  Result.Data := AData;
  FItems.Add(Result)
end;

procedure TDataItem.AddList(ADatas: TList);
var
  i:  Integer;
begin
  for i := 0 to ADatas.Count - 1 do Add(ADatas[i]);
end;

constructor TDataItem.Create;
begin
  FItems := TList.Create;
  FProps := TStringList.Create;
end;

destructor TDataItem.Destroy;
begin
  FItems.Free;
  FProps.Free;
  inherited;
end;

function TDataItem.GetProp(AName: string): TDataItem;
var
  idx:    integer;
begin
  result := nil;
  idx := FProps.IndexOfName(AName);
  if idx <> -1 then
    Result := TDataItem(FProps.Objects[idx]);
end;

procedure TDataItem.SetDeleted(AValue: Boolean);
var
  i:  integer;
begin
  FDeleted := AValue;
  for i := 0 to FProps.Count - 1 do
    TDataItem(FProps.Objects[i]).Deleted := AValue;
  for i := 0 to FItems.Count - 1 do
    TDataItem(FItems[i]).Deleted := AValue;
end;

procedure TDataItem.SetProp(AName: string; AData: TDataItem);
var
  idx:    integer;
begin
  idx := FProps.IndexOfName(AName);
  if idx = -1 then
    FProps.AddObject(AName, AData)
  else
    FProps.Objects[idx] := AData;
end;

end.
